import request from '@/utils/request'
import { SAVE_PROFILE, SAVE_USER } from '../constants/profile'

export const saveUser = (payload) => {
  return {
    type: SAVE_USER,
    payload,
  }
}

export const getUser = () => {
  return async (dispatch) => {
    const res = await request.get('user')
    dispatch(saveUser(res.data))
  }
}

export const saveProfile = (payload) => {
  return {
    type: SAVE_PROFILE,
    payload,
  }
}

export const getProfile = () => {
  return async (dispatch) => {
    const res = await request.get('user/profile')
    dispatch(saveProfile(res.data))
  }
}

export const updateProfile = (data) => {
  return async (dispatch) => {
    await request({
      url: 'user/profile',
      method: 'PATCH',
      data,
    })
    dispatch(getProfile())
  }
}

export const updateAvatar = (data) => {
  return async (dispatch) => {
    await request({
      url: 'user/photo',
      method: 'PATCH',
      data,
    })
    dispatch(getProfile())
  }
}
